package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.myExceptions.AssociatedException;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用服务层分页查询方法
        return addressService.pageQuery(currentPage, pageSize, queryString);
    }


    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();//03a36073-a140-4942-9b9b-712cecb144901.jpg
            //获取文件名后缀，先获取 . 的索引位置
            int index = originalFilename.lastIndexOf(".");
            //获取文件后缀，截取字符串
            String extention = originalFilename.substring(index - 1);
            //使用UUID随机生成文件名称，防止文件重名
            String fileName = UUID.randomUUID().toString() + extention;
            //调用七牛工具类上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将图片名称上传到redis(setmealPicResources)中
            jedisPool.getResource().sadd(RedisConstant.ADDRESS_PIC_RESOURCES, fileName);
            //上传成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            //上传失败
            return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            System.out.println(address);
            addressService.add(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAILL);
        }
        return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
    }


    //根据ID删除
    @RequestMapping("/delete")
    public Result deleteById(Integer id, String imgName) {
        try {
            //调用服务层的删除
            addressService.delete(id);
            //调用工具类删除七牛云上的图片
            if (imgName!=null){
                QiniuUtils.deleteFileFromQiniu(imgName);
            }
        } catch (Exception e) {
             e.printStackTrace();
            if (e instanceof AssociatedException){
                return new Result(false, "检测机构被预约,不允许删除");
            }
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
    }

    @RequestMapping("/edit")
public Result edit(@RequestBody Address address){
    try {
        System.out.println(address);
        addressService.edit(address);
    } catch (Exception e) {
        e.printStackTrace();
        return new Result(false, MessageConstant.EDIT_ADDRESS_FALL);
    }
    return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
}
}
